#! /usr/bin/env python
# -*- python -*-

#----------------------------------------------------------------
# References:
#   The calculations used in this program are all from the site
#   http://www.vb-helper.com/tutorial_platonic_solids.html
#   Many thanks for making this information available!
#
#----------------------------------------------------------------

import math
import os






def PREPARE_FILES():

#----------------------------------------------------------------------
#---
#---    This module prompts for the input file and opens the input
#---    and output files.
#---
#---    The format of the INPUT filename is the PATH and the FILENAME
#---    but without the extension: C:\PATH\Filename
#---    All backup filenames created by adding the .BKP or .TMP
#---    extension to this root name
#----------------------------------------------------------------------


    
    print "This Program Inputs a Free-Formated File and Outputs a Fixed-Formated File."

    #---  Set the parameters needed for prompting for the input HEAD.ASC file.

    INPUT_NAME = "INPUT"   

    INPUT_NAME = raw_input("Enter name of the Free-Formated input file [.ASC]: ")
    #C
    #C---  Prompt the user for the input input free-formatted file.
    #C
    INPUT_NAME_PATH = './' + str(INPUT_NAME) + '.TMP'

    if os.path.isfile( INPUT_NAME_PATH ):
        try:
            f = open(INPUT_NAME_PATH,"r")

            print f.readline()

        except:
            print "Error: INPUT filename return"


    #      CALL GET_FILE( INPUT_NAME, MSG, 0, EXIT_PROG ) 
    #      WRITE(*,*) 'Return Input filename: ', INPUT_NAME
    #      IF( EXIT_PROG ) STOP 'Error: INPUT filename return'


    NEWINPUT_NAME = INPUT_NAME + '.TMP'
    print "New Input Filename: ", NEWINPUT_NAME

    INPUT_BACKUP = INPUT_NAME + '.BKP'

    OPEN_NAME = INPUT_NAME + '.ASC'

    onf = open(OPEN_NAME,"r")

    onif = open(NEWINPUT_NAME,"r")

    #C---  Set the parameters needed for prompting for the OUTPUT file.

    DIRPATH = os.getcwd()

    CADIN_NAME = DIRPATH + '\\' + INPUT_NAME + '.ASC'

    print "CADIN file: " + CADIN_NAME

    #C
    #C---  The output file backup is the user's input file name with the
    #C---  extenstion ".TP5"; this file will be located in the default directory.
    #C

    CADIN_BACKUP = INPUT_NAME + '.TP5'
    #C---  Open the CADIN AND ERROR files.      

    ICADIN = open(CADIN_NAME,"r")

    #C---  Open the error file.  But first build directory and path 
    #C---  from OUTPUT FILE data.

    ERROR_FILE = DIRPATH + '\ERROR.ASC'

    print "Error File: ", ERROR_FILE

    JERROR = open( ERROR_FILE, "r")


    return


#C----------------------------------------------------------------------
#C
#C     This module copies the variables and their C locations from the
#C     HEAD.ASC file to a working file.  Expanding the arrays, and
#C     completing arrays notation where necessary.
#C----------------------------------------------------------------------

def COPY_HEAD():
    print "* * * Expanding HEAD.ASC file * * *"

    #C---  Open the  Copied HEAD file.
    NUHEAD = open( "NUHEAD.ASC", "r")

    #C---  Open the original HEAD.ASC file.
    GOODFILE = False

    PATHDIREC = os.getcwd()

    FNAME = 'HEAD.ASC'

    FILENAME = PATHDIREC + '\\' + FNAME


    if os.path.isfile( FILENAME ):
        print "Opening HEAD File: ", FILENAME

        IHEAD = open(FILENAME,"r")

    else:

        # If HEAD.ASC exists then use it, otherwise prompt for user filename

        while GOODFILE == False:

            NEW_FNAME = raw_input("Please enter name of HEADER file: ")

            FILENAME = PATHDIREC + "\\" + NEW_FNAME + '.ASC'

            if os.path.isfile( FILENAME ):
                try:           
                    IHEAD = open(FILENAME,"r") 
                    
                    print "Opening HEAD File: ", FILENAME

                    GOODFILE = True

                except IOError:
                    print IOError

            else:
                print FILENAME + ' not found'                


    # Read the file
    ln = IHEAD.readline()
    lnu = ln.upper()
    VAR_READ = lnu[13:25]

    LPAREN = VAR_READ.find('(')
    IKSEE = VAR_READ.find('KSEE')

    for O_ASTR in IHEAD.readlines():

        COMMENT = O_ASTR[25:80]               #! Save the comment.

        print "Comment: ", COMMENT
        LEN_COMM = COMMENT.__len__()
        
        ASTRING = O_ASTR.upper()

        #IF( LEN_COMM .EQ. 0 ) LEN_COMM = 1

        VAR_READ = ASTRING[12:25]

        print "VAR_READ: ", VAR_READ

        LPAREN = int(VAR_READ.find('(' ))
        IKSEE = VAR_READ.find('KSEE')

        if (LPAREN > 0):
            if (IKSEE > 0): 
                print "LKSEE > 0"
                pass

            LOC_IN_C = int(ASTRING[5:8])

            RPAREN = VAR_READ.find(')')

            if (RPAREN > 0):
              IEND = RPAREN
            else:
              IEND = VAR_READ.__len__()
            
            KOMMA = VAR_READ.find(',' )

            if (KOMMA > 0):

              IROW = int(VAR_READ[LPAREN+1:KOMMA])
              ICOL = int(VAR_READ[KOMMA+1:RPAREN])

              print "IROW: ", IROW
              print "ICOL: ", ICOL
            else:
              IROW = (VAR_READ[LPAREN+1:IEND])
              ICOL = 0
              print "IROW: ", IROW



            # VAR_READ variable name
            VAR_READ_N = VAR_READ[0:LPAREN]

            print "VAR_READ: ", VAR_READ_N

            if( ICOL == 0 ):
                pass

    return


#-----------------------------------
# Main program

print "/**********************************************************/"
print " *     CADIN3.PY                                          *"
print " *     Version 0.1                                        *"
print " *     http://ausrockets.blogspot.com.au                  *"
print "/**********************************************************/"




PREPARE_FILES()

COPY_HEAD()
